package com.example.demo.api;

import lombok.extern.log4j.Log4j2;
import org.apache.ibatis.javassist.NotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: GlobalExceptionHandler
 * @Description: GlobalExceptionHandler
 * @Author: zzz
 * @Date: 2020-04-04 21:45
 */
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

    /**
     * @Author: zzz
     * @Date: 2020/4/5 15:52
     * @Description:
     * @param: request
     * @param: e 
     * @return: com.example.demo.api.ResponseResult
     */
    @ExceptionHandler(value=Exception.class)
    public ResponseResult allExceptionHandler(HttpServletRequest request,Exception e)
    {
//        System.out.println("我报错了："+exception.getLocalizedMessage());
//        System.out.println("我报错了："+exception.getCause());
//        System.out.println("我报错了："+exception.getSuppressed());
//        System.out.println("我报错了："+exception.getMessage());
//        System.out.println("我报错了："+exception.getStackTrace());
        return new ResponseResult(HttpStatus.INTERNAL_SERVER_ERROR.value(),e.getMessage());
    }

    /**
     * @Author: zzz
     * @Date: 2020/4/5 15:55
     * @Description:
 * @param: e
 * @return: com.example.demo.api.ResponseResult
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseResult handleMethodArgumentNotValidException(MethodArgumentNotValidException e){

        return new ResponseResult(HttpStatus.BAD_REQUEST.value(),e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
    }

}